package com.qqzone.controller;

import com.qqzone.pojo.Topic;
import com.qqzone.pojo.UserBasic;
import com.qqzone.service.TopicService;
import com.qqzone.service.UserBasicService;

import java.util.List;

/**
 * @author Yang
 * @creat 11:26 2022/8/25
 * @sice 1.8
 */

public class UserController {

    private UserBasicService userBasicService;
    private TopicService topicService;

    public String login(String loginId, String pwd, javax.servlet.http.HttpSession session) {
        // 1.登录验证
        UserBasic userBasic = userBasicService.login(loginId, pwd);
        if (userBasic != null) {
            List<UserBasic> friendList = userBasicService.getFriendList(userBasic);
            List<Topic> topicList = topicService.getTopicList(userBasic);

            userBasic.setFriendList(friendList);
            userBasic.setTopicList(topicList);

            // userBasic 这个key保存的是登录者的信息
            // friend 这个key保存的是当前进入的是谁的空间
            session.setAttribute("userBasic", userBasic);
            session.setAttribute("friend", userBasic);
            return "index";
        } else {
            return "login";
        }
    }

    public String user(javax.servlet.http.HttpSession session) {
        UserBasic userBasic = (UserBasic) session.getAttribute("userBasic");
        session.setAttribute("friend",userBasic);
        return "index";
    }

    public String friend(Integer id, javax.servlet.http.HttpSession session) {
        // 获取好友日志
        UserBasic userBasic = userBasicService.getUserBasicById(id);
        List<Topic> topicList = topicService.getTopicList(userBasic);

        userBasic.setTopicList(topicList);

        session.setAttribute("friend", userBasic);

        return "index";
    }
}
